﻿ 
using SHKF.B2C.BLL;
using SHKF.B2C.BLL.Center;
using SHKF.B2C.BLL.LM;
using SHKF.B2C.MeiTuanShanGou.Model.Entity.Order;
using SHKF.B2C.Model;
using SHKF.B2C.Model.Entity.Center;　
using SqlSugar;
using System;
using System.Collections.Generic;
　　
namespace SHKF.B2C.MeiTuanShanGou.BLL
{
    public class MtsgOrderBLL : BaseBll
    {
        private readonly ShopMapBLL shopBll;
        private readonly OrderIDBLL orderIDBLL;

        public MtsgOrderBLL(SqlSugarClient db,
             ShopMapBLL shopBll,
             OrderIDBLL orderIDBLL ) :
           base(db)
        { 
            this.db.ChangeDatabase(DB_Mtsg_LM);
            this.shopBll = shopBll;
            this.orderIDBLL = orderIDBLL;
        }

        public bool IsExist(long app_poi_code,long orderID)
        {
            this.db.ChangeDatabase("PtE_Mtsg_LM");

           bool bOK = this.db
                .Queryable<OrderContentEntity>()
                .Where(ww => ww.app_poi_code == app_poi_code && ww.order_id == orderID)
                .Any();

            return bOK;
        }

        public OrderContentEntity Query(long app_poi_code, long orderID)
        {
            OrderContentEntity order = this.db
                .Queryable<OrderContentEntity>()
                .Where(ww => ww.app_poi_code == app_poi_code && ww.order_id == orderID)
                .First();

            return order;
        }

        public List<OrderContentEntity> Query(long shopSN, int status, DateTime create_start, DateTime create_end, int logistics_status)
        {
            var shops = this.shopBll.Query(shopSN, PlatformEnum.Mtsg);
            if (shops.Count > 0)
            {
                List<OrderContentEntity> list = new List<OrderContentEntity>();
                foreach (ShopMapEntity shop in shops)
                {
                    string strCmdMtsg = string.Format(@"select * from PtE_{0}_LM.dbo.OrderFormContent  where app_poi_code = {1}", shop.Platform, shop.app_poi_code);
                    var sql = db.SqlQueryable<OrderContentEntity>(strCmdMtsg);
                    sql.Where(w => w.ctime >= create_start
                      && w.ctime <= create_end
                      && w.logistics_status == logistics_status);

                    if (status != 0)
                    {
                        sql.Where(w => w.status == status);
                    }
                    var list2 = sql.ToList();
                    if (list2 != null)
                    {
                        list.AddRange(list2);
                    }
                }

                return list;
            } 
            return new List<OrderContentEntity>();
        }

        public List<OrderDetailEntity> QueryDetail(long FKSN)
        {
            var freesql = db.Queryable<OrderDetailEntity>()
               .Where(w => w.FKSN == FKSN);

            return freesql.ToList();
        }
         
        public void SaveAll(OrderContentEntity orderEntity, List<OrderDetailEntity> orderDetails, List<SHKF.B2C.Model.Entity.Order.OrderAccountEntity> orderAccounts)
        {
            try
            {
                this.db.BeginTran();

                bool bExist = false;

                //保存定单
               long SN = this.SaveOrder(orderEntity, out bExist);
               
                //保存明细
                if (orderDetails != null)
                {
                    foreach (OrderDetailEntity orderDetail in orderDetails)
                    {
                        orderDetail.FKSN = SN;
                        this.SaveDetail(orderDetail);
                    }
                }

                this.db.CommitTran();

                //保存账目
                this.db.ChangeDatabase("PtE_LM");

                foreach (SHKF.B2C.Model.Entity.Order.OrderAccountEntity account in orderAccounts)
                {
                    account.OrderSN = SN;
                    account.pDate = DateTime.Now;
                    SHKF.B2C.Model.Entity.Order.OrderAccountEntity oldAccount = this.db
                        .Queryable<SHKF.B2C.Model.Entity.Order.OrderAccountEntity>()
                        .Where(ww => ww.OrderSN == SN && ww.KeyWord == account.KeyWord && ww.Title == account.Title)
                        .First();

                    if (oldAccount == null) {

                        this.db
                            .Insertable<SHKF.B2C.Model.Entity.Order.OrderAccountEntity>(account)
                            .ExecuteCommand(); 
                    }
                    else
                    {
                        account.SN = oldAccount.SN; 
                        this.db
                            .Updateable<SHKF.B2C.Model.Entity.Order.OrderAccountEntity>(account)
                            .ExecuteCommand();
                    }
                }

               

            }
            catch (Exception ex)
            {
                this.db.RollbackTran();
                throw ex;
            }

        }

        protected long SaveOrder(OrderContentEntity order, out bool bExist)
        {
            long SN = 0;
            bExist = false;
            try
            { 
                OrderContentEntity orderOld = this.db.Queryable<OrderContentEntity>()
                    .Where(oo => oo.app_poi_code == order.app_poi_code
                                  && oo.order_id == order.order_id)
                    .First();

                order.pDate = DateTime.Now; 
                if (orderOld != null)
                {
                    SN = orderOld.SN;
                    order.SN = SN; 
                    int iResult = this.db
                        .Updateable<OrderContentEntity>(order)
                        .ExecuteCommand();

                    bExist = true;
                }
                else
                {
                    SN = this.orderIDBLL.GetOrderID();
                    order.SN = SN; 
                    this.db.Insertable<OrderContentEntity>(order)
                        .ExecuteCommand(); 
                    bExist = false;
                }　
            }
            catch (Exception ex)
            { 
                throw ex;
            }
            return SN;
        }

        protected long SaveDetail(OrderDetailEntity orderDetail)
        {
            long SN = 0;
            OrderDetailEntity orderDetailEntity = db.Queryable<OrderDetailEntity>()
                .Where(oo => oo.FKSN == orderDetail.FKSN && oo.item_id == orderDetail.item_id)
                .First();
            if (orderDetailEntity != null)
            {
                orderDetail.SN = orderDetailEntity.SN;
                int iResult = db.Updateable<OrderDetailEntity>(orderDetail)
                   .ExecuteCommand();
            }
            else
            {
                SN = db.Insertable<OrderDetailEntity>(orderDetail)
                      .ExecuteReturnBigIdentity();
            }

            return SN;
        } 

    }
}
